#!/bin/sh
if [ $# -ne 1 ]; then
  echo Usage: $0 pkgname
  exit 1
fi
error=0
pkgname=`basename $1`
if [ ! -f "/var/db/pkg/$pkgname/+CONTENTS" ]; then
  echo "$pkgname not found"
  exit 1
fi
cd /usr/local
for file in `grep -v ^@ /var/db/pkg/$pkgname/+CONTENTS | sort -u `; do
  echo $file | grep -q ^+
  if [ $? -eq 0 ]; then
    continue
  fi
  if [ ! -e $file ]; then
    echo "$file not found"
    error=1
    continue
  fi
  expect=`grep -A 1 ^$file$ /var/db/pkg/$pkgname/+CONTENTS | sort -u | awk -F: '{print $2}' | xargs echo -n`
  actual=`md5 $file | awk '{print $NF}'`
  if [ -L "$file" ]; then
    continue
  fi
  if [ ! "$expect" = "$actual" ]; then
    error=1
    echo "$file mismatch: E:[$expect] A:[$actual]"
  fi
done
exit $error
